package com.xqh.admin.controller.ques;

import com.xqh.admin.commom.BaseController;
import com.xqh.base.mybatis.page.Page;
import com.xqh.base.util.response.RespMsg;
import com.xqh.base.util.serializer.ObjectUtils;
import com.xqh.entity.ques.QuesType;
import com.xqh.entity.ques.Question;
import com.xqh.service.ques.QuesTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 题型
 */
@Controller
@RequestMapping(value = "/ques/type")
public class QuesTypeController extends BaseController {

    @Autowired
    private QuesTypeService typeService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView list(){
        return new ModelAndView("ques/type/ques_type_list");
    }

    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public ModelAndView edit(ModelMap model){
        String parentCode = getString("parentCode");
        if(StringUtils.hasText(parentCode)){
            Map<String,Object> map = new HashMap<>();
            map.put("code", parentCode);
            QuesType parent = typeService.findOneByBiz(map);
            model.put("parent", parent);
        }
        if(StringUtils.hasText(getString("id"))){
            QuesType data = typeService.findById(Long.valueOf(getString("id")));
            model.put("data", data);
        }
        return new ModelAndView("ques/type/ques_type_edit");
    }

    @RequestMapping(value = "listData")
    public @ResponseBody
    Page<?> listData(Page page, QuesType type){
        typeService.findByPage(page, ObjectUtils.changeToMap(type));
        return page;
    }

    @RequestMapping(value = "treeData", method = RequestMethod.POST)
    public @ResponseBody Object treeData(QuesType type){
       return typeService.treeData(type);
    }

    @RequestMapping(value = "saveData", method = RequestMethod.POST)
    public @ResponseBody
    RespMsg<?> saveData(QuesType dto){
        try {
            putOperatorInfo(dto);
            typeService.saveData(dto);
        }catch (Exception e){
            return RespMsg.failResp(e.getMessage());
        }
        return RespMsg.successResp(getExtMap("callbackMethod"));
    }

    /**
     * 获取题型数据
     * @param type
     * @return
     */
    @RequestMapping(value = "findData", method = RequestMethod.POST)
    @ResponseBody
    public RespMsg<?> findData(QuesType type){
       List<QuesType> typeList = typeService.findByBiz(ObjectUtils.changeToMap(type));
       return RespMsg.successResp(typeList);
    }
}
